package ae.pcfc.etks.online.itworx.ehs.wf.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.ws.client.core.WebServiceTemplate;

import ae.pcfc.etks.online.itworx.ehs.wf.exception.WFException;
import ae.pcfc.etks.online.itworx.ehs.wf.service.WFService;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.ArrayOfString;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.CreateProcess;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.CreateProcessResponse;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.DoAction;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.DoActionResponse;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetActiveTasks;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetActiveTasksResponse;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetDelegatedUsers;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetDelegatedUsersResponse;
//import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetTaskSerialNumber;
//import ae.pcfc.etks.online.itworx.ehs.wf.service.types.GetTaskSerialNumberResponse;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.IsOfficer;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.IsOfficerResponse;
import ae.pcfc.etks.online.itworx.ehs.wf.service.types.K2WorklistItem;

public class WFServiceImpl implements WFService {

	protected WebServiceTemplate wsTemplate;
	protected Map<String, String> webServicesEndPointsMap;
	protected String classURIValue;
	
	/**
	 * @param wsTemplate the wsTemplate to set
	 */
	@Resource(name="wfWSTemplate")
	public void setWsTemplate(WebServiceTemplate wsTemplate) {
		this.wsTemplate = wsTemplate;
	}
	
	/**
	 * @param webServicesEndPointsMap the webServicesEndPointsMap to set
	 */
	@Resource(name="wfWebServicesEndPointsMap")
	public void setWebServicesEndPointsMap(
			Map<String, String> webServicesEndPointsMap) {
		this.webServicesEndPointsMap = webServicesEndPointsMap;
	}
	
	public int createProcess(String processName, List<String> parameterNames, List<String>  parameterValues) throws WFException {
		
		CreateProcess createProcessInput = new CreateProcess();
		createProcessInput.setProcessFullName(processName);
		ArrayOfString names = new ArrayOfString();
		ArrayOfString values = new ArrayOfString();
		names.getString().addAll(0, parameterNames);
		values.getString().addAll(0, parameterValues);
		createProcessInput.setParameterNames(names);
		createProcessInput.setParameterValues(values);
		
		String classKey = StringUtils.delete(
				ClassUtils.getShortName(this.getClass()), "Impl");
		classURIValue = webServicesEndPointsMap.get(classKey);
		
		CreateProcessResponse createProcessOutput = (CreateProcessResponse) wsTemplate.marshalSendAndReceive(classURIValue, createProcessInput);
		return createProcessOutput.getCreateProcessResult();
	}

	public boolean isOfficer(String officerName) throws WFException {
		IsOfficer isOfficerReq = new IsOfficer();
		isOfficerReq.setUserName(officerName);
		String classKey = StringUtils.delete(
				ClassUtils.getShortName(this.getClass()), "Impl");
		classURIValue = webServicesEndPointsMap.get(classKey);
		IsOfficerResponse isOfficerRes = (IsOfficerResponse) wsTemplate.marshalSendAndReceive(classURIValue, isOfficerReq);
		return isOfficerRes.isIsOfficerResult();
	}
	
	//@Override
	/*public String getTaskSerialNumber(int processId,String userName) throws WFException
	{
		GetTaskSerialNumber getTaskSerialNumInput=new GetTaskSerialNumber();
		getTaskSerialNumInput.setProcessID(processId);
		getTaskSerialNumInput.setUserName(userName);
		
		String classKey = StringUtils.delete(
				ClassUtils.getShortName(this.getClass()), "Impl");
		classURIValue = webServicesEndPointsMap.get(classKey);
		
		GetTaskSerialNumberResponse getTaskSerialNumResponse = (GetTaskSerialNumberResponse) wsTemplate.marshalSendAndReceive(classURIValue, getTaskSerialNumInput);
		return getTaskSerialNumResponse.getGetTaskSerialNumberResult();
	}
	*/
	public boolean doAction(String processId, String userName, String action, List<String> parameterNames, List<String> parameterValues, boolean isOfficer) throws WFException {
		DoAction doActionInput = new DoAction();
		doActionInput.setAction(action);
		doActionInput.setProcessID(Integer.parseInt(processId));
		doActionInput.setUserName(userName);
		doActionInput.setIsOfficer(isOfficer);
		ArrayOfString names = new ArrayOfString();
		ArrayOfString values = new ArrayOfString();
		names.getString().addAll(0, parameterNames);
		values.getString().addAll(0, parameterValues);
		doActionInput.setParameterNames(names);
		doActionInput.setParameterValues(values);
		String classKey = StringUtils.delete(
				ClassUtils.getShortName(this.getClass()), "Impl");
		System.out.println("Size:::::::::" + values.getString().size());
		classURIValue = webServicesEndPointsMap.get(classKey);
		DoActionResponse doActionOutput = (DoActionResponse) wsTemplate.marshalSendAndReceive(classURIValue, doActionInput);
		return doActionOutput.isDoActionResult();
	}

	public List<K2WorklistItem> getActiveTasks(String processName, String userName, boolean isOffecier, boolean isAssigned) throws WFException {
		
		GetActiveTasks getActiveTasksInput = new GetActiveTasks();
		getActiveTasksInput.setProcessName(processName);
		getActiveTasksInput.setUserName(userName);
		getActiveTasksInput.setIsOfficer(isOffecier);
		getActiveTasksInput.setIsAssigned(isAssigned);
		
		String classKey = StringUtils.delete(
				ClassUtils.getShortName(this.getClass()), "Impl");
		classURIValue = webServicesEndPointsMap.get(classKey);
		
		GetActiveTasksResponse getActiveTasksOutput = (GetActiveTasksResponse) wsTemplate.marshalSendAndReceive(classURIValue, getActiveTasksInput);
		return getActiveTasksOutput.getGetActiveTasksResult().getK2WorklistItem();
	}
	

	public String[] getDelegatedUsers(String userName){
		GetDelegatedUsers getDelegatedUsersInput = new GetDelegatedUsers();
		getDelegatedUsersInput.setUserName(userName);
		
		String classKey = StringUtils.delete(ClassUtils.getShortName(this.getClass()), "Impl");
		classURIValue = webServicesEndPointsMap.get(classKey);
		
		GetDelegatedUsersResponse getDelegatedUsersOutput = (GetDelegatedUsersResponse) wsTemplate.marshalSendAndReceive(classURIValue, getDelegatedUsersInput);
		List<Object> result = getDelegatedUsersOutput.getGetDelegatedUsersResult().getAnyType();
		String[] users = new String[result.size()];
		for(int i = 0;i < result.size();i++){
			users[i] = (result.get(i) == null)? null : result.get(i).toString();
			if(users[i] != null && users[i].contains("\\")){
				users[i] = users[i].substring(users[i].lastIndexOf('\\')+1);
			}
		}
		return users;
	}

}
